package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.SubidWorkorder;
import com.mt.roll.core.dal.service.SubidWorkorderServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.SubidWorkorderVoMapper;
import com.mt.roll.wms.params.*;
import com.mt.roll.wms.service.SubidWorkorderService;
import com.mt.roll.wms.vo.SubidWorkorderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2022/3/10 15:49
 */

@Service
@Transactional
public class SubidWorkorderServiceImpl extends BaseService implements SubidWorkorderService {

    @Autowired
    private SubidWorkorderServiceBiz subidWorkorderServiceBiz;

    @Autowired
    private SubidWorkorderVoMapper subidWorkorderVoMapper;

    @Override
    public R<PageVo<SubidWorkorderVo>> page(SubidWorkorderQueryParam subidWorkorderQueryParam) {
        IPage<SubidWorkorderVo> page = subidWorkorderVoMapper.page(new Page<>(subidWorkorderQueryParam.getCurrent(),subidWorkorderQueryParam.getSize()),subidWorkorderQueryParam);
        return successful(new PageVo<>(page, SubidWorkorderVo.class));
    }

    @Override
    public R<SubidWorkorderVo> getById(IdParam idParam) {
        SubidWorkorder subidWorkorder  = subidWorkorderServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", subidWorkorder );
        SubidWorkorderVo subidWorkorderVo = subidWorkorderVoMapper.getById(idParam.getId());
        return successful(subidWorkorderVo);
    }

    @Override
    public R<IdVo> add(SubidWorkorderParam subidWorkorderParam) {
        SubidWorkorder subidWorkorder = new SubidWorkorder();
        BeanUtils.copyProperties(subidWorkorderParam, subidWorkorder);
        setCommonField(subidWorkorder);
        subidWorkorderServiceBiz.save(subidWorkorder);
        return successful(IdVo.builder().id(subidWorkorder.getId()).build());
    }

    @Override
    public R<IdVo> update(SubidWorkorderParam subidWorkorderParam) {
        SubidWorkorder subidWorkorder = subidWorkorderServiceBiz.getById(subidWorkorderParam.getId());
        SubidWorkorder updateAlarm = new SubidWorkorder();
        BeanUtils.copyProperties(subidWorkorderParam, updateAlarm);
        setUpdateCommonField(updateAlarm);
        subidWorkorderServiceBiz.updateById(updateAlarm);
        return successful(IdVo.builder().id(subidWorkorder.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        SubidWorkorder subidWorkorder = subidWorkorderServiceBiz.getById(idParam.getId());
        if (subidWorkorder == null) {
            return failed("信息不存在");
        }
        subidWorkorderServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(subidWorkorder.getId()).build());
    }

}
